package cn.pzhuweb.controller;

import cn.pzhuweb.pojo.Message;
import cn.pzhuweb.service.CarService;
import cn.pzhuweb.service.imp.CarServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Yancy xiao
 * @create 2022-12-21 20:21
 */
@WebServlet("/querycar")
public class QueryCarServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryCarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id = 0;
        try {
            id = Integer.parseInt(request.getParameter("id"));//username来自于json的参数名
        } catch (Exception e) {
            request.getSession().setAttribute("error", "id不是数字");
            response.sendRedirect("error.jsp");
            return;
        }
        CarService carService=new CarServiceImp();
        Message message = carService.isUsed(id);
        response.getWriter().print(message.getMsg());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
